import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

/*
Draws the desired token board given through the constructor method.
*/
public class TokenBoardPopup extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Launch the application.
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		TokenBoard tokens = new TokenBoard();
		tokens.setToken("/gallery/m&m.jpg");
		tokens.setToken("/gallery/skittle.jpg");
		tokens.setToken("/gallery/m&m.jpg");
		tokens.giveReward(0);
		tokens.giveReward(0);
		tokens.giveReward(0);
		tokens.giveReward(1);
		tokens.giveReward(1);
		tokens.giveReward(2);
		TokenBoardPopup board = new TokenBoardPopup(tokens);
		JFrame test = new JFrame();
		test.setBounds(100, 100, 760, 500);

		test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel contentPane = new JPanel();
		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		
		test.setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		JPanel panel_1 = board;
		panel.add(panel_1);
		panel_1.setAlignmentX(Component.RIGHT_ALIGNMENT);
		
		panel.add(Box.createHorizontalGlue());

		JButton btnNewButton = new JButton("Continue");
		btnNewButton.setAlignmentX(Component.RIGHT_ALIGNMENT);
		panel.add(btnNewButton);

		//test.add(board);
		test.repaint();
		test.setVisible(true);
		test.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		System.out.println(tokens.getTokens());
	}

	/**
	 * Create the dialog.
	 * @throws IOException 
	 */
	public TokenBoardPopup(TokenBoard tokens) throws IOException {
		
		ImageIcon image = new ImageIcon();
		IconResizer resizer = new IconResizer(image);
		int maxtokens = tokens.getTokenNum();
		int[] drawn = new int[maxtokens/10];
		//setBounds(100, 100, 758, 498);
		setLayout(new GridLayout(0, drawn.length));
		//for (int i = 0; i < 6; i++ ){
			//this.add(new JButton("HI!"));
		//}
		
		//Set all drawn tokens to 0;
		for (int i = 0; i < drawn.length; i++){
			drawn[i] = 0;
		}
		//draw empty spaces or tokens
		for (int i=0; i < maxtokens; i++){
			JButton added = new JButton();
			added.setBorderPainted(false);
			added.setContentAreaFilled(false);
			//draw tokens if there's only 2 tokens available.
			if (maxtokens == 20){
				if (i%2 == 0 && drawn[0]<tokens.getNumToken(0)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(0))));
					drawn[0]++;
				}
				else if ((i-1)%2 == 0 && drawn[1]<tokens.getNumToken(1)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(1))));
					drawn[1]++;
				}
				else image = new ImageIcon(ImageIO.read( TokenBoardPopup.class.getResourceAsStream("/gallery/circle.jpg" )));
			}
			//draw tokens if there's only 3 tokens available.
			else if (maxtokens == 30){
				if (i%3 == 0 && drawn[0]<tokens.getNumToken(0)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(0))));
					drawn[0]++;
				}
				else if ((i-1)%3 == 0 && drawn[1]<tokens.getNumToken(1)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(1))));
					drawn[1]++;
				}
				else if ((i-2)%3 == 0 && drawn[2]<tokens.getNumToken(2)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(2))));
					drawn[2]++;
				}
				else image = new ImageIcon(ImageIO.read( TokenBoardPopup.class.getResourceAsStream("/gallery/circle.jpg" )));
			}
			//draw tokens if there's only 4 tokens available.
			else if (maxtokens == 40){
				if (i%4 == 0 && drawn[0]<tokens.getNumToken(0)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(0))));
					drawn[0]++;
				}
				else if ((i-1)%4 == 0 && drawn[1]<tokens.getNumToken(1)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(1))));
					drawn[1]++;
				}
				else if ((i-2)%4 == 0 && drawn[2]<tokens.getNumToken(2)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(2))));
					drawn[2]++;
				}
				else if ((i-3)%4 == 0 && drawn[3]<tokens.getNumToken(3)){
					image = new ImageIcon(ImageIO.read(new File(tokens.getRewardImage(3))));
					drawn[3]++;
				}
				else image = new ImageIcon(ImageIO.read( TokenBoardPopup.class.getResourceAsStream("/gallery/circle.jpg" )));
			}
			/*
			if (i%4 == 0 && atokendrawn<tokens.getNumTokenA()){
				image = new ImageIcon(tokens.reinforcera.getImage());
				atokendrawn++;
			}
			else if ((i-1)%4 == 0 && btokendrawn<tokens.getNumTokenB()){
				image = new ImageIcon(tokens.reinforcerb.getImage());
				btokendrawn++;
			}
			else image = new ImageIcon("/Developer/SrDesign/src/gallery/circle.jpg");
			*/
			resizer.setImage(image);
			resizer.resize(25, 25);
			added.setIcon(resizer.getImage());
			//getContentPane().add(added);
			this.add(added);
		}
	}

}
